﻿#region summary

// Copyright (c)  MOKEYISH. All rights reserved.
// Licensed under the MIT License, See License.txt in the project root for license information.

#endregion

using System;

namespace NSQL.Expressions
{
    public class ComparisonExpression : BaseExpression, IComparisonExpression
    {
        public IExpression Left { get; set; }

        public IExpression Right { get; set; }

        public ComparisonOperator Operator { get; set; }

        public ComparisonExpression(IExpression leftExpression, IExpression rightExpression,
            ComparisonOperator op)
        {
            Left = leftExpression;
            Right = rightExpression;
            Operator = op;
        }

        public static ComparisonOperator ParseOperator(string op)
        {
            switch (op)
            {
                case "=": return ComparisonOperator.Equal;
                case "<>":
                case "^=":
                case "~=":
                case "!=": return ComparisonOperator.NotEqual;
                case ">": return ComparisonOperator.Greater;
                case "<": return ComparisonOperator.Less;
                case ">=": return ComparisonOperator.GreaterOrEqual;
                case "<=": return ComparisonOperator.LessOrEqual;
                default:
                    throw new Exception("UnExpected relational operator.expecting {=, <>, ^=, ~=, !=, >, <, >=, <=}");
            }
        }

        public override T Accept<T>(IExpressionVisitor<T> visitor) => visitor.Visit(this);
    }
}